#ifndef MYGLWIDGET_H
#define MYGLWIDGET_H

#include <QOpenGLWidget>
#include <QOpenGLFunctions>
#include <QOpenGLShaderProgram>
#include <QOpenGLFramebufferObject>
#include <QOpenGLBuffer>
#include <QTimer>
#include <QElapsedTimer>

class MyGLWidget : public QOpenGLWidget, protected QOpenGLFunctions
{
    Q_OBJECT

public:
    MyGLWidget(QWidget *parent = nullptr);
    ~MyGLWidget();

protected:
    void initializeGL() override;
    void resizeGL(int w, int h) override;
    void paintGL() override;

private slots:
    void updateDynamicContent();

private:
    void init_fbo();
    void init_vbo_and_ebo();
    void init_texture();
    void init_double_pbo();

    void setupVertexAttribs();
    void renderToFBO();
    void renderToScreen();
    void updatePartialArea(int x, int y, int width, int height, const void *data);

    QOpenGLShaderProgram shaderProgram;
    QOpenGLFramebufferObject *fbo;
    GLuint textureID;
    QOpenGLBuffer vbo;
    GLuint ebo;
    GLuint pbo[2]; // 双缓冲 PBO
    GLuint currentPBO; // 当前使用的 PBO
    QTimer *updateTimer;

    QElapsedTimer m_elapsed_timer;

    float rectangleSize;

    int region_x;
    int region_y;
    int region_width;
    int region_height;
};

#endif // MYGLWIDGET_H

